﻿using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Web.Mvc;
using PhotoBlogApp.Models;
using PhotoBlogApp.Models.Builders;
using PhotoBlogApp.Models.ClientModels;
using PhotoBlogApp.Models.Photos;
using PhotoBlogApp.Models.Providers;

namespace PhotoBlogApp.Controllers
{
    public class RootController : Controller
    {
        private readonly IPhotoService photoService;
        private readonly IRequestService requestService;
        private readonly IAlbumProvider albumProvider;
        private readonly IBlogProvider blogProvider;
        private readonly IDataCache dataCache;

        private static readonly string Title = ConfigurationManager.AppSettings["Title"];
        private static readonly string VirtualPathRootName = ConfigurationManager.AppSettings["VirtualPathRootName"];
        private static readonly string CatalogPortfolioName = ConfigurationManager.AppSettings["CatalogPortfolioName"];
        private readonly IMentionService mentionService;

        public RootController(
            IPhotoService photoService,
            IRequestService requestService,
            IAlbumProvider albumProvider,
            IBlogProvider blogProvider, 
            IMentionService mentionService, 
            IDataCache dataCache)
        {
            this.photoService = photoService;
            this.requestService = requestService;
            this.albumProvider = albumProvider;
            this.blogProvider = blogProvider;
            this.mentionService = mentionService;
            this.dataCache = dataCache;
        }

        public void ReloadChache()
        {
            dataCache.GetAlbums();
            dataCache.GetPosts();            
        }

        public ActionResult Index(string message)
        {
            ViewBag.Title = Title;
            ViewBag.Message = message;
            return View();
        }

        [AllowAnonymous]
        public ActionResult GetPhotos(GetPhotosParams photosParams)
        {
            ViewBag.Title = Title;
            var mapPath = Server.MapPath("~/" + VirtualPathRootName) + "\\" + CatalogPortfolioName;
            var photos = photoService.BuildPhotos(photosParams);
            photos.Temp = mapPath;
            return Json(photos);
        }

        [HttpPost]
        public ActionResult SendRequest(string name, string fam, string coordinates, string comments)
        {
            var request = new RequestM
            {
                Name = name,
                Fam = fam,
                Comments = comments,
                Coordinates = coordinates
            };
            var result = requestService.Validate(request);
            string m;
            if (result.IsSuccess)
            {
                requestService.Save(request);
                SendMail(request);
                m = "Заявка успешно отправлена";
            }
            else
            {
                m = "Заявка не была отправлена";
            }
            return RedirectToAction("Index", new { message = m });
        }

        private void SendMail(RequestM request)
        {
            using (var smtpClient = new SmtpClient())
            {
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.Port = 587;
                smtpClient.Credentials = new NetworkCredential("mshatikhin@gmail.com", "@PkPv29masoc");
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.Timeout = 20000;

                var body = string.Format("Заявка от: {0} {1}. <br/> Координаты: {2}. <br/> Комментарии: {3}.", request.Name, request.Fam, request.Coordinates, request.Comments);
                var mail = new MailMessage
                {
                    From = new MailAddress("mshatikhin@gmail.com", "365cg.ru"),
                    Body = body,
                    IsBodyHtml = true,
                    Subject = "Заявка с сайта 365cg.ru",
                    Priority = MailPriority.High
                };

                mail.To.Add(new MailAddress("mshatikhin@gmail.com"));

                smtpClient.Send(mail);
            }
        }

        public ActionResult Main()
        {
            ViewBag.Title = Title;
            return View("Index");
        }

        public ActionResult Portfolio()
        {
            ViewBag.Title = Title;
            var result = new AlbumsM
            {
                AlbumMs = albumProvider.GetAlbums()
            };
            return View("Portfolio", result);
        }

        public ActionResult Blog()
        {
            ViewBag.Title = Title;
            var model = new PostsM
            {
                Posts = blogProvider.GetPosts()
            };
            return View("Blog", model);
        }

        public ActionResult About()
        {
            ViewBag.Title = Title;
            return View("About");
        }

        public ActionResult SendMention(string name, string mention)
        {                    
            mentionService.Add(name, mention);
            return RedirectToAction("Index");
        }

    }
}